package com.example.xmpic.ui.adapter;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.common.utils.LiveDataBus;
import com.example.xmpic.R;
import com.example.xmpic.R2;
import com.example.xmpic.support.config.TimeItem;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author skygge
 * @date 2020/7/6.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：
 */
public class AlarmPeriodAdapter extends RecyclerView.Adapter<AlarmPeriodAdapter.ItemHolder> {

    private Context mContext;
    private List<TimeItem> timeList;

    public AlarmPeriodAdapter(Context mContext, List<TimeItem> list) {
        this.mContext = mContext;
        this.timeList = list;
    }

    public void update(int position, TimeItem data) {
        timeList.set(position, data);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_alarm_period, viewGroup, false);
        return new ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemHolder itemHolder, int i) {
        TimeItem info = timeList.get(i);
        String title = mContext.getString(R.string.time) + (i + 1);
        itemHolder.alarmTitle.setText(title);
        itemHolder.alarmTime.setText(info.getTime());
        if (info.isOpen()){
            itemHolder.alarmState.setText(info.getWeeks(mContext));
        }else {
            itemHolder.alarmState.setText(mContext.getString(R.string.disable));
        }

        itemHolder.itemView.setOnClickListener(v -> {
            LiveDataBus.get().with("FUN_ALARM_TIME").setValue(i);
        });
    }

    @Override
    public int getItemCount() {
        return timeList == null ? 0 : timeList.size();
    }

    static class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R2.id.alarm_title)
        TextView alarmTitle;
        @BindView(R2.id.alarm_time)
        TextView alarmTime;
        @BindView(R2.id.alarm_state)
        TextView alarmState;
        public ItemHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
